package com.qqtech.qquan.mall.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.mall.dao.MallProductSpecDao;
import com.qqtech.qquan.mall.model.MallProductSpec;
import com.qqtech.qquan.mall.model.MallProductSpecQuery;
import com.qqtech.qquan.mall.model.MallProductSpecVo;

@Service
public class MallProductSpecServiceImpl extends BaseServiceImpl<MallProductSpec>
		implements MallProductSpecService {
	public static final Logger log = LoggerFactory.getLogger(MallProductSpecServiceImpl.class);
	
	@Resource
	private MallProductSpecDao mallProductSpecDao;

	@Override
	protected BaseDao<MallProductSpec> getBaseDao() {
		return mallProductSpecDao;
	}

	@Override
	public List<MallProductSpecVo> queryListByProductId(Integer productId) {
		MallProductSpecQuery query = new MallProductSpecQuery();
		query.setProductId(productId);
		query.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(new PageOrder(Direction.DESC, "id"));
		List<MallProductSpecVo> list = mallProductSpecDao.queryList(query, pageKit);
		return list;
	}

	@Override
	public int handleReduceStock(int specId, int quantity) {
		 return mallProductSpecDao.reduceStock(specId, quantity);
	}

	@Override
	public int handleReturnStock(int specId, int quantity) {
		Preconditions.checkArgument(quantity > 0);
        return mallProductSpecDao.addStock(specId, quantity);
	}
	
}